package com.deyuanyun.pic.ae.service.impl;

import com.deyuanyun.pic.ae.controller.dto.AEBaseQuery;
import com.deyuanyun.pic.ae.controller.dto.PressObjectEdit;
import com.deyuanyun.pic.ae.controller.dto.SewerQuery;
import com.deyuanyun.pic.ae.domain.PressObject;
import com.deyuanyun.pic.ae.domain.PressObjectExample;
import com.deyuanyun.pic.ae.domain.Sewer;
import com.deyuanyun.pic.ae.domain.SewerExample;
import com.deyuanyun.pic.ae.enumeration.DataStatedEnum;
import com.deyuanyun.pic.ae.enumeration.IsNewDateEnum;
import com.deyuanyun.pic.ae.mapper.PressObjectMapper;
import com.deyuanyun.pic.ae.mapper.SewerMapper;
import com.deyuanyun.pic.ae.mapper.dao.PressObjectDao;
import com.deyuanyun.pic.ae.mapper.dao.SewerDao;
import com.deyuanyun.pic.ae.service.AEBaseService;
import com.deyuanyun.pic.ae.service.PressObjectService;
import com.deyuanyun.pic.ae.service.SewerService;
import com.deyuanyun.pic.common.util.CommonUtil;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.pipe.domain.Identification;
import com.deyuanyun.pic.pipe.util.PipeMgrUtil;
import com.deyuanyun.pic.settings.support.biz.AttachmentUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * “管道占压物” 服务接口 实现类
 *
 * @author huanglw
 * @date  2016-11-12
 *
 * History: [修改人][时间][修改描述]
 */
@Service
public class PressObjectServiceImpl extends AEBaseService implements PressObjectService {

    @Resource
    private PressObjectMapper pressObjectMapper;
    @Resource
    private PressObjectDao pressObjectDao;

    @Override
    public Page<PressObject> query(AEBaseQuery aeBaseQuery) {
        return pressObjectDao.query(aeBaseQuery);
    }

    @Override
    public PressObject find(String id) {
        return pressObjectMapper.selectByPrimaryKey(id);
    }

    @Override
    public int edit(PressObject pressObject, List<String> imgs) {

        // 管道占压 只有点状，没有线状数据
        Identification beginIdentification = PipeMgrUtil.queryIdentificationFlag(pressObject.getNagmInput());
        Asserts.assertLogic((beginIdentification==null), "附近测试桩:“"+pressObject.getNagmInput()+"”不存在，请重新输入");
        pressObject.setNagmInput(beginIdentification.getId());
        pressObject.setNagmNumberInput(beginIdentification.getSignNumber());

        // 计算绝对里程
        PositionObj crossPositionObj = getPositionInfo(pressObject.getPipeLineId(), pressObject.getPositionX(), pressObject.getPositionY());
        if (crossPositionObj != null) {
            pressObject.setMileage(crossPositionObj.getMileage());
            pressObject.setNagm(crossPositionObj.getNagm());
            pressObject.setNagmNumber(crossPositionObj.getNagmNumber());
            pressObject.setNagmMileage(crossPositionObj.getNagmMileage());
        }

        // 添加 图片信息
        String bizzId = StringUtils.isNotNullEmpty(pressObject.getId()) ? pressObject.getId() : CommonUtil.getUUID();
        for (String id : imgs) {
            AttachmentUtil.updateBusinessId(bizzId, id);
        }

        if (StringUtils.isNullEmpty(pressObject.getId())){
            PressObjectExample pressObjectExample = new PressObjectExample();
            pressObjectExample.createCriteria().andDeletedEqualTo(Short.valueOf("0")).andNameEqualTo(pressObject.getName());
            int countNum = pressObjectMapper.countByExample(pressObjectExample);
            Asserts.assertLogic(countNum > 0, "名称为:“"+pressObject.getName()+"”管道占压物 已经存在，不能重复插入");

            pressObject.setId(bizzId);
            pressObject.setCreateDate(new Date());
            pressObject.setCreateUser(realName);
            pressObject.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
            pressObject.setIsNewest(Short.valueOf(IsNewDateEnum.YES.getCode()));
            return pressObjectMapper.insertSelective(pressObject);
        }

        PressObject pressObjectObj = this.find(pressObject.getId());
        Asserts.assertLogic(pressObjectObj == null, "名称为:“"+pressObject.getName()+"”管道占压物 不存在，不允许修改");
        Asserts.assertLogic(isAuditReleased(pressObjectObj.getDataStated()), "名称为:“"+pressObject.getName()+"”管道占压物 已审核通过,不允许编辑");
        if (isAuditDismissed(pressObjectObj.getDataStated())){
            pressObject.setDataStated(DataStatedEnum.ALREADY_RELEASED.getCode());
        }
        return pressObjectMapper.updateByPrimaryKeySelective(pressObject);
    }

    @Override
    public int audit(PressObject pressObject) {
        PressObject pressObjectObj = this.find(pressObject.getId());
        Asserts.assertLogic(pressObjectObj == null, "名称为:“"+pressObject.getName()+"”管道占压物 不存在，不允许修改");
        Asserts.assertLogic(isAudit(pressObjectObj.getDataStated()), "名称为:“"+pressObject.getName()+"”管道占压物 已审核,不允许重复审核");
        return pressObjectMapper.updateByPrimaryKeySelective(pressObject);
    }

    @Override
    public int delete(String id) {
        PressObject pressObject = this.find(id);
        Asserts.assertLogic(pressObject == null, "名称为:“"+pressObject.getName()+"”管道占压物 不存在，不允许删除");
        Asserts.assertLogic(isAuditReleased(pressObject.getDataStated()), "名称为:“"+pressObject.getName()+"”管道占压物 已审核通过,不允许删除");
        pressObject = new PressObject();
        pressObject.setId(id);
        pressObject.setDeleted(Short.valueOf("1"));
        pressObject.setUpdateDate(new Date());
        pressObject.setUpdateUser(realName);
        pressObject.setUpdateRemark("删除");
        return pressObjectMapper.updateByPrimaryKeySelective(pressObject);
    }

}
